package org.zxyj.core.entity;

import java.io.Serializable;
import java.util.List;

/**
 * 分页处理
 * 
 * @author zhaoyong
 * @version 2016年2月18日
 */
public class Pagination implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2261419623304303162L;

	/**
	 * 默认分页大小
	 */
	public final static int DEFAULT_PAGESIZE = 10;
	public final static int PAGESIZE1 = 1;
	
	/**
	 * 当前页的请求参数名
	 */
	public final static String CURRENT_KEY = "pagination.current";
	
	/**
	 * 当前页的请求参数名
	 */
	public static final String PAGESIZE_KEY = "pagination.pageSize";

	/**
	 * 当前页
	 */
	private int current = 1;

	/**
	 * 记录总数
	 */
	private Long total = 0l;

	/**
	 * 一页多少条记录
	 */
	private int pageSize = DEFAULT_PAGESIZE;

	/**
	 * 偏移量
	 */
	private int offset = 0;

	/**
	 * 总页数
	 */
	private int totalPages = 1;

	/**
	 * 数据集合
	 */
	private List<?> datas;

	/**
	 * 当前页的记录数量
	 */
	private int currentPageCount;
	
	/**
	 * 数据库数据起始偏移量
	 * 
	 * @return int
	 */
	public int getOffset() {
		this.offset = (this.current - 1) * pageSize;
		return offset;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current < 1 ? 1 : (this.totalPages > 1 && current > this.totalPages ? this.totalPages : current);
	}

	public Long getTotal() {
		return total;
	}

	public void setTotal(Long total) {
		this.total = total;
		if (this.total == 0) {
			this.totalPages = 1;
			return;
		}
		this.totalPages = (int) (this.total / this.pageSize);
		if (this.total % this.pageSize != 0) {
			this.totalPages++;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public List<?> getDatas() {
		return datas;
	}

	public void setDatas(List<?> datas) {
		this.datas = datas;
	}

	public int getCurrentPageCount() {
		if (datas != null) {
			return datas.size();
		}
		
		return 0;
	}

}